package org.noote.libs.assistant.helper;

import org.noote.libs.assistant.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.widget.TextView;

public class CheckSpeechCapacities_Activity extends Activity {
	private static final int MY_TTS_CHECK_CODE = 0;
	private static final int MY_RS_CHECK_CODE = 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_tts_main);
 
        // First check TTS
        checkTTS();
	}
	
	private void checkTTS() {
    	TextView textViewTTS = (TextView) findViewById(R.id.textViewTTS);
    	textViewTTS.setText(getApplicationContext().getString(R.string.text_checking));
		
		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkIntent, MY_TTS_CHECK_CODE);		
	}
	
	private void checkSR()
	{
    	TextView textViewSR = (TextView) findViewById(R.id.textViewSR);
    	textViewSR.setText(getApplicationContext().getString(R.string.text_checking));

		if(SpeechRecognizer.isRecognitionAvailable(getApplicationContext()))
		{
        	textViewSR.setText(getApplicationContext().getString(R.string.text_checked));
			
			ended();
		}
		else
		{
        	textViewSR.setText(getApplicationContext().getString(R.string.text_retrieving));
        	
			// TODO Start get SpeechRecognizer service from Market/Google Play
			
			Intent goToMarket = new Intent(Intent.ACTION_VIEW)
		    	.setData(Uri.parse("market://details?id=com.google.android.voicesearch"));
			startActivityForResult(goToMarket, MY_RS_CHECK_CODE);
		}
	}
	
	private void ended()
	{
		this.setResult(Activity.RESULT_OK);
		this.finish();
	}

	
	@Override
	protected void onActivityResult(
	        int requestCode, int resultCode, Intent data) {
	    if (requestCode == MY_TTS_CHECK_CODE) {
	        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {

	        	TextView textViewTTS = (TextView) findViewById(R.id.textViewTTS);
	        	textViewTTS.setText(getApplicationContext().getString(R.string.text_checked));
	        	
	        	// TODO check French and English data set !
	        	
	        	// Second check SR !
	        	checkSR();
	        	
	        } else {
	        	TextView textViewSR = (TextView) findViewById(R.id.textViewTTS);
	        	textViewSR.setText(getApplicationContext().getString(R.string.text_retrieving));			
	        	
	            // missing data, install it
	            Intent installIntent = new Intent();
	            installIntent.setAction(
	                TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
	            startActivity(installIntent);
	        }
	    }
		else if (requestCode == MY_RS_CHECK_CODE) {
			TextView textViewSR = (TextView) findViewById(R.id.textViewSR);
			if(SpeechRecognizer.isRecognitionAvailable(getApplicationContext()))
			{
	        	textViewSR.setText(getApplicationContext().getString(R.string.text_checked));
				
				ended();
			}
			else
			{
	        	textViewSR.setText(getApplicationContext().getString(R.string.text_unavailable));
			}			
		}
	}
 }
